<template>
  <div class="selectDiv">
    <span v-if="store.state.customer.loginId>0" style="color:rgba(8,157,32,0.96);">[在线]</span>
    <span v-else style="color:rgba(126,17,32,0.96);">[离线]</span>
    <span>客服：</span>
    <app-avatar :size="40" :src="store.state.customer.user.avatarUrl"></app-avatar>
    <el-select style="margin-left: 10px;flex: 1" v-model="item" placeholder="Select" size="large">
      <el-option
          v-for="it in options"
          :key="it.id"
          :label="it.nickName"
          :value="it.id"
      />
    </el-select>
  </div>
</template>

<script lang="ts" setup>

import {onMounted, ref, watch} from "vue";
import {queryCustomerAccount} from "@/api/customer";
import {ICustomerInfo} from "@/api/types/customer";
import {store} from "@/store";

const item = ref<number>(0)
const options = ref<Array<ICustomerInfo>>()

onMounted(async () => {
  const {rows} = await queryCustomerAccount({pageNum: 1, pageSize: 100})
  options.value = rows
  if (rows.length > 0) {
    item.value = rows[0].id!
  }
})
// 监听当前的客服
watch(item, () => {
  const user = options.value!.find(i => {
    return i.id === item.value
  })!
  // 客服登录
  store.commit('customer/setCustomerUser',user)
  // 每次切换相当模拟重新登录-所以清空
  store.state.customer.activeUser = null
})


</script>

<style lang="scss" scoped>
.selectDiv {
  display: flex;
  justify-content: flex-start;
  align-items: center;

  & > span {
    width: 50px;
    line-height: 40px;
    font-weight: bold;
  }
}
</style>